/**
 * 
 */
package org.assembly.fenrir.model.student;

import java.util.Calendar;
import java.util.Date;

import org.hibernate.validator.NotNull;
import org.hibernate.validator.Past;


/**
 * class that represents a student
 * 
 * @author emanuel
 *
 */
public class Student {
	
	private static final int COUNT_MONTH_IN_YEAR = 12;

	private String name;
	
	private String lastName;
	
	private Date birthDate;
	
	private String comment;

	public Student(String name, String lastName, Date birthDate){
		this.name = name;
		this.lastName = lastName;
		this.birthDate = birthDate;
	}
	
	public Student() {	}

	
	@NotNull
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@NotNull
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@NotNull
	@Past 
	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public Long getAge() {
		Calendar calendarBirth = Calendar.getInstance();
		calendarBirth.setTime(this.birthDate);
		Calendar now = Calendar.getInstance();
		Long age = null;
		
		if (calendarBirth.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
			if (calendarBirth.get(Calendar.DATE) <= now.get(Calendar.DATE)) {
				age = new Long(now.get(Calendar.YEAR) - calendarBirth.get(Calendar.YEAR));
			} else {
				// birthday not be yet... 
				age = new Long(now.get(Calendar.YEAR) - calendarBirth.get(Calendar.YEAR)) - 1;
			}
		} else {
			if (calendarBirth.get(Calendar.MONTH) < now.get(Calendar.MONTH)) {
				age = new Long(now.get(Calendar.YEAR) - calendarBirth.get(Calendar.YEAR));
			} else {
				// birthday not be yet...
				age = new Long(now.get(Calendar.YEAR) - calendarBirth.get(Calendar.YEAR)) - 1;
			}
		}
		
		return age;
	}
	
	public Long getMonthsFromBirth() {
		Calendar calendarBirth = Calendar.getInstance();
		calendarBirth.setTime(this.birthDate);
		Calendar now = Calendar.getInstance();
		Long month =  new Long(now.get(Calendar.MONTH) - calendarBirth.get(Calendar.MONTH));
		
		if (calendarBirth.get(Calendar.MONTH) > now.get(Calendar.MONTH)) {
			month = 12 + month;
		} else {
			if (calendarBirth.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
				if (calendarBirth.get(Calendar.DATE) > now.get(Calendar.DATE)) {
					month = 11l;					
				}
			} 
		}
		
		return month;
	}
	
	public Long getCountMonths() {	
		return this.getAge() * COUNT_MONTH_IN_YEAR + this.getMonthsFromBirth(); 
	}

}
